Listview with card

  • Steps

    Model

    model structure

    
                          class City {
    
                          //--- Name Of City
                          final String name;
                          //-- image
                          final String image;
                          //--- population
                          final String population;
                          //--- country
                          final String country;
    
                          City({this.name,this.country,this.population,this.image}); 
    
    
                         
    
    
    
                          }
    
                          

    function in model

    
                          static List<City> allCities()
                          {
                            var lstOfCities = new List<City>();
    
                            lstOfCities.add(new City(name:"Delhi",country: "India",population: "19 mill",image: "delhi.png"));
                            lstOfCities.add(new City(name:"London",
                                            country: "Britain",population: "8 mill",image: "london.png"));
                            lstOfCities.add(new City(name:"Vancouver",
                                            country: "Canada",population: "2.4 mill",image: "vancouver.png"));
                            lstOfCities.add(new City(name:"New York",
                                            country: "USA",population: "8.1 mill",image: "newyork.png"));
                            lstOfCities.add(new City(name:"Paris",
                                            country: "France",population: "2.2 mill",image: "paris.png"));
                            lstOfCities.add(new City(name:"Berlin",
                                            country: "Germany",population: "3.7 mill",image: "berlin.png"));
                            return lstOfCities;
                          }
    
                        

    home page view

    
                          import 'package:flutter/material.dart';
    import 'package:flutter4_listview/model/city.dart';
    
    class HomePage extends StatelessWidget {
     
    final List<City> _allCities = City.allCities();
    
    HomePage() {}
    
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
          appBar: new AppBar(
            title: new Text(
              "Cites around world",
              style: new TextStyle(
                  fontSize: 18.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87),
            ),
          ),
          body: new Padding(
              padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
              child: getHomePageBody(context)));
    }
    
    getHomePageBody(BuildContext context) {
      return ListView.builder(
        itemCount: _allCities.length,
        itemBuilder: _getItemUI,
        padding: EdgeInsets.all(0.0),
      );
    }
      // First Attempt
      Widget _getItemUI(BuildContext context, int index) {
        return new Text(_allCities[index].name);
      }
    }
    
    

    main view

    
    import 'package:flutter/material.dart';
    import 'package:flutter4_listview/pages/homepage.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
            primarySwatch: Colors.amber,
          ),
          home: new HomePage(),
        );
      }
    }
    
    

    Image

    in pubspec.yaml

    
     assets:
      - assets/berlin.png
      - assets/delhi.png
      - assets/london.png
      - assets/newyork.png
      - assets/paris.png
      - assets/vancouver.png
    

    _getItemUi()

    
    Widget _getItemUI(BuildContext context, int index) {
      return new Card(
          child: new Column(
        
        children: <Widget>[
          new ListTile(
            leading: new Image.asset(
              "assets/" + _allCities[index].image,
              fit: BoxFit.cover,
              width: 100.0,
            ),
    
            title: new Text(
              _allCities[index].name,
              style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold),
            ),
            subtitle: new Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Text(_allCities[index].country,
                      style: new TextStyle(
                          fontSize: 13.0, fontWeight: FontWeight.normal)),
                  new Text('Population: ${_allCities[index].population}',
                      style: new TextStyle(
                          fontSize: 11.0, fontWeight: FontWeight.normal)),
                ]),
            //trailing: ,
            onTap: () {
              _showSnackBar(context, _allCities[index]);
            },
          )
        ],
      ));
    }
    
    

    onTap

    
    _showSnackBar(BuildContext context, City item) {
      final SnackBar objSnackbar = new SnackBar(
        content: new Text("${item.name} is a city in ${item.country}"),
        backgroundColor: Colors.amber,
      );
      Scaffold.of(context).showSnackBar(objSnackbar);
    }